<template>
    <van-button :icon="value ===-1 ? 'good-job-o':'good-job'"
     :class="value ===-1 ? 'dislike':'like'"
    :loading="loading"
    @click="onLike"
    />

      <!-- <van-icon
         color="#777"
         :name="value?'star-o':'star'"
       /> -->
</template>

<script>
import { addLike, deleteLike } from '@/api/article'
export default {
  name: 'LikeArticle',
  components: {},
  props: {
    value: {
      type: Number,
      required: true
    },
    articleId: {
      type: [Number, String, Object],
      required: true
    }
  },
  data () {
    return {
      loading: false
    }
  },
  computed: {},
  watch: {},
  created () {},
  mounted () {},
  methods: {
    async  onLike () {
      this.loading = true
      try {
        if (this.value === -1) {
          await addLike(this.articleId)
        } else {
          await deleteLike(this.articleId)
        }
        this.$emit('input', (this.value === -1 ? 1 : -1))
        this.$toast.success(this.value === -1 ? '点赞成功' : '取消点赞')
      } catch (err) {
        let message = '操作失败，请重试！'
        if (err.response && err.response.status === 401) {
          message = '用户未登录，请先登录！'
        }
        this.$toast.fail(message)
      }
      this.loading = false
    }

  }
}
</script>

<style scoped lang="less">
.like{
   .van-icon {
   color: #e5645f;

}
}
.dislike{
   .van-icon{
       color: #777;
   }
}

</style>
